package com.beiding.controller.manager;

import com.beiding.exception.BadRequestException;
import com.beiding.pojo.manager.Application;
import com.beiding.service.Authorities;
import com.beiding.service.email.EmailService;
import com.beiding.service.manager.ManagerService;
import com.beiding.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/manager")
public class ManagerRestController {

    private EmailService emailService;

    private UserService userService;


    private ManagerService managerService;

    @Autowired
    public void setManagerService(ManagerService managerService) {
        this.managerService = managerService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    @PostMapping("/apply-for-manager")
    void applyForManager(Principal principal, String authority, String text, HttpServletRequest request) {


        Assert.isTrue(text.length()<50,"申请正文过长");

        GrantedAuthority grantedAuthority = Authorities.ofManager(authority);

        //首先校验是否支持该权限的申请
        if (grantedAuthority == null) {
            throw new BadRequestException("无效的管理员权限");
        }

        String email = userService.getEmail("admin");

        emailService.sendApplyForManager(email, principal.getName(), grantedAuthority, text);

    }


    @GetMapping("/accredit")
    String accredit(String token) {

        //通过令牌获取申请
        Application application = emailService.getApplication(token);

        if (application == null) {
            return "无效的令牌或已过期";
        }

        userService.addAuthority(application.getProposer(), application.getAuthority());

        //将被授予权限的用户登记
        managerService.register(application.getProposer(), application.getAuthority().getAuthority());

        //使令牌失效
        emailService.invalidateToken(token);

        //通知申请人已经授予权限
        String email = userService.getEmail(application.getProposer());

        if (email != null) {
            emailService.send(email, "授权通知", "你申请的'" + Authorities.toText(application.getAuthority()) + "'权限已被授权,刷新管理员入口页面查看");
        }

        return "授权成功";
    }




    //撤回用户权限
    @PostMapping("/authority/withdraw")
    void withdraw(String username, String authority) {
        managerService.withdraw(username, authority);
    }


    //获取admin用户所赋予的所有权限.
    @GetMapping("/authority/all")
    Map<String, Set<Authorities.Name>> all() {
        Map<String, Set<String>> all = managerService.getAll();

        Map<String, Set<Authorities.Name>> r = new HashMap<>();

        //将权限转换为名称返回
        all.forEach((s, strings) -> {
            r.put(s, Authorities.toNames(strings));
        });

        return r;
    }




}
